package com.ics.atable.chat.controller;

import com.ics.atable.chat.common.BaseResponse;
import com.ics.atable.chat.common.ResultUtils;
import com.ics.atable.chat.model.dto.ItemImportDTO;
import com.ics.atable.chat.service.ItemService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 指标管理控制器
 *
 * @author Chawu
 */
@Slf4j
@RestController
@RequestMapping("/item")
@Tag(name = "指标管理", description = "指标相关接口")
public class ItemController {

    @Autowired
    private ItemService itemService;

    /**
     * 上传Excel文件并处理指标数据
     *
     * @param file Excel文件
     * @return 处理结果
     */
    @PostMapping("/upload")
    @Operation(summary = "上传Excel文件并Embedding", description = "上传包含指标数据的Excel文件，支持.xlsx格式")
    public BaseResponse<List<ItemImportDTO>> uploadExcel(@RequestParam("file") MultipartFile file) {
        try {
            // 调用service层处理Excel文件
            List<ItemImportDTO> itemList = itemService.processExcelFile(file);
            
            log.info("Excel文件处理成功，共处理{}条数据", itemList.size());
            return ResultUtils.success(itemList);

        } catch (IllegalArgumentException e) {
            log.error("文件验证失败: {}", e.getMessage());
            return ResultUtils.error(400, e.getMessage());
        } catch (Exception e) {
            log.error("处理Excel文件失败", e);
            return ResultUtils.error(500, "处理Excel文件失败: " + e.getMessage());
        }
    }
}
